package com.stone.serverless.beetl.service;

import com.stone.serverless.beetl.ExposeServiceManager;
import com.stone.serverless.beetl.loader.SeClassLoaderKit;
import com.stone.serverless.orm.service.MapperRegisterService;
import org.beetl.sql.clazz.kit.ClassLoaderKit;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/4/6 9:20
 * @Version: 1.0.0
 */
@Component
public class MapperRegisterServiceImpl implements MapperRegisterService, InitializingBean {

    public static SQLManager sqlManager;


    @Override
    public <T> T register(Class<T> baseMapper) {
        ClassLoaderKit classLoaderKit = sqlManager.getClassLoaderKit();
        if (classLoaderKit instanceof SeClassLoaderKit) {
            ((SeClassLoaderKit) classLoaderKit).setClassLoader(baseMapper.getClassLoader());
        }

        T mapper = sqlManager.getMapper(baseMapper);
        return mapper;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ExposeServiceManager.INSTANCE.add(this);
    }
}
